<template>
  <div>
    <div class="title">
      <h1>排放系数</h1>
    </div>
    <div class="content">
      <br>
      <br>
      <el-form ref="form" :model="form" label-width="200px">
        <el-col :span="8"><div class="grid-content bg-purple">排入水中的化学物质</div></el-col>
        <br>
        <br>
        <el-form-item
          label="固着度："
          style="width: 600px;font-size: 8ex"
          prop="fixation"
          :rules="[
            {required: true, message: '固着度不能为空',trigger: 'blur' }
          ]"
        >
          <el-input v-model="form.fixation" style="width: 100%;" placeholder="请输入内容(0~1范围内)" :disabled="disabledInput_1">
            <template slot="append">（百分比请换算成小数填入）</template>
          </el-input>
          <el-button round @click="changeInput_1">自定义</el-button>
          <el-input v-show="changed_1==0" v-model="input_2" placeholder="请给出依据" />
        </el-form-item>

        <el-form-item
          label="残液比："
          style="width: 600px;font-size: 8ex"
          prop="residualLiquor"
          :rules="[
            {required: true, message: '残液比不能为空',trigger: 'blur' }
          ]"
        >
          <el-input v-model="form.residualLiquor" style="width: 100%;" placeholder="请输入内容(0~1范围内)" :disabled="disabledInput_2">
            <template slot="append">（百分比请换算成小数填入）</template>
          </el-input>
          <el-button round @click="changeInput_2">自定义</el-button>
          <el-input v-show="changed_2==0" v-model="input_4" placeholder="请给出依据" />
        </el-form-item>
        <el-col :span="8"><div class="grid-content bg-purple">排入大气中的化学物质</div></el-col>
        <br>
        <br>
        <el-form-item
          label="排放系数："
          style="width: 600px;font-size: 8ex"
          prop="airPfxs"
          :rules="[
            {required: true, message: '排放系数不能为空',trigger: 'blur' }
          ]"
        >
          <el-input v-model="form.airPfxs" style="width: 100%;" placeholder="请输入内容(0~1范围内)" :disabled="disabledInput_3">
            <template slot="append">（百分比请换算成小数填入）</template>
          </el-input>
          <el-button round @click="changeInput_3">自定义</el-button>
          <el-input v-show="changed_3==0" v-model="input_6" placeholder="请给出依据" />
        </el-form-item>

        <el-col :span="8"><div class="grid-content bg-purple">排入土壤中的化学物质</div></el-col>
        <br>
        <br>
        <el-form-item
          label="排放系数："
          style="width: 600px;font-size: 8ex"
          prop="soilPfxs"
          :rules="[
            {required: true, message: '排放系数不能为空',trigger: 'blur' }
          ]"
        >
          <el-input v-model="form.soilPfxs" style="width: 100%;" placeholder="请输入内容(0~1范围内)" :disabled="disabledInput_4">
            <template slot="append">（百分比请换算成小数填入）</template>
          </el-input>
          <el-button round @click="changeInput_4">自定义</el-button>
          <el-input v-show="changed_4==0" v-model="input_8" placeholder="请给出依据" />
        </el-form-item>
        <el-form-item
          label="依据文件："
          style="width: 600px;font-size: 8ex"
        >
          <el-upload
            ref="upload"
            class="upload-demo"
            action=""
            multiple
            accept=".pdf"
            :http-request="uploadPDF"
            :on-preview="handlePreview"
            :on-remove="handleRemove"
            :file-list="fileList"
            :auto-upload="false"
          >
            <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
            <el-button style="margin-left: 10px;" size="small" type="success" @click="submitUpload">上传到服务器</el-button>
            <div slot="tip" class="el-upload__tip">只能上传PDF文件，且不超过50MB</div>
          </el-upload>
        </el-form-item>
      </el-form>
      <el-button style="margin-top: 12px;" @click="goBack">上一步</el-button>
      <el-button style="margin-top: 12px;" @click="sendEmissionFactor">下一步</el-button>
    </div>

  </div>

</template>

<script>
import DirectUsage from './UsageParameters/DirectUsage'
import IndirectUsage from './UsageParameters/IndirectUsage'
import BaseInformation from './BaseInformation'
import evaluate from '../../../store/modules/evaluate'
import axios from 'axios'

export default {
  name: 'EmissionFactor',
  data() {
    return {
      fileList: [],
      form: {
        fixation: '',
        residualLiquor: '',
        airPfxs: '',
        soilPfxs: ''
      },
      input_2: '',
      input_4: '',
      input_6: '',
      input_8: '',
      disabledInput_1: true,
      disabledInput_2: true,
      disabledInput_3: true,
      disabledInput_4: true,
      flag_1: 0,
      flag_2: 0,
      flag_3: 0,
      flag_4: 0,
      changed_1: 1,
      changed_2: 1,
      changed_3: 1,
      changed_4: 1,
      evaluate: {}
    }
  },
  watch: { // 使用这个 属性，可以监视 data 中指定数据的变化，然后触发这个 watch 中对应的 function 处理函数
    'input_1': function(newVal, oldValue) {
      if (parseFloat(newVal) < 0 || parseFloat(newVal) > 1) {
        this.form.fixation = ''
        this.$message({
          message: '数值只能在0-1之间',
          type: 'warning'
        })
      }
    },
    watch: { // 使用这个 属性，可以监视 data 中指定数据的变化，然后触发这个 watch 中对应的 function 处理函数
      'form.fixation': function(newVal, oldValue) {
        if (parseFloat(newVal) < 0 || parseFloat(newVal) > 1) {
          this.form.fixation = ''
          this.$message({
            message: '数值只能在0-1之间',
            type: 'warning'
          })
        }
      },
      'form.residualLiquor': function(newVal, oldValue) {
        if (parseFloat(newVal) < 0 || parseFloat(newVal) > 1) {
          this.form.residualLiquor = ''
          this.$message({
            message: '数值只能在0-1之间',
            type: 'warning'
          })
        }
      },
      'form.airPfxs': function(newVal, oldValue) {
        if (parseFloat(newVal) < 0 || parseFloat(newVal) > 1) {
          this.form.airPfxs = ''
          this.$message({
            message: '数值只能在0-1之间',
            type: 'warning'
          })
        }
      },
      'form.soilPfxs': function(newVal, oldValue) {
        if (parseFloat(newVal) < 0 || parseFloat(newVal) > 1) {
          this.form.soilPfxs = ''
          this.$message({
            message: '数值只能在0-1之间',
            type: 'warning'
          })
        }
      }
    },
    'input_5': function(newVal, oldValue) {
      if (parseFloat(newVal) < 0 || parseFloat(newVal) > 1) {
        this.form.airPfxs = ''
        this.$message({
          message: '数值只能在0-1之间',
          type: 'warning'
        })
      }
    },
    'input_7': function(newVal, oldValue) {
      if (parseFloat(newVal) < 0 || parseFloat(newVal) > 1) {
        this.form.soilPfxs = ''
        this.$message({
          message: '数值只能在0-1之间',
          type: 'warning'
        })
      }
    }
  },
  methods: {
    uploadPDF(param) {
      const casNo = this.$store.getters.getEvaluate.casNo
      const fileObject = param.file
      const formData = new FormData()
      formData.append('file', fileObject)
      formData.append('cas', casNo)
      axios.post('http://localhost:8083/evaluate/upload', formData, { headers: { 'Content-Type': 'multipart/form-data' }}).then((res) => {
        console.log(res)
        if (res.data.code == 200) {
          this.$message('文件上传成功')
        } else { this.$message('上传失败，请重试') }
      })
    },
    submitUpload() {
      this.$refs.upload.submit()
    },
    handleRemove(file, fileList) {
      console.log(file, fileList)
    },
    handlePreview(file) {
      console.log(file)
    },
    goBack() {
      this.$emit('goBack')
    },
    changeInput_1() {
      this.flag_1 = 1
      this.changed_1 = 0
      this.disabledInput_1 = false
    },
    changeInput_2() {
      this.flag_2 = 1
      this.changed_2 = 0
      this.disabledInput_2 = false
    },
    changeInput_3() {
      this.flag_3 = 1
      this.changed_3 = 0
      this.disabledInput_3 = false
    },
    changeInput_4() {
      this.flag_4 = 1
      this.changed_4 = 0
      this.disabledInput_4 = false
    },
    getPfxs() {
      this.evaluate = this.$store.getters.getEvaluate
      // 获取废水排放系数
      const pfxsParams = { wzlb: this.evaluate.wzlb, process: this.evaluate.process, fiberId: this.evaluate.fiberId }
      this.$api.evaluate.getWaterPfxs(pfxsParams).then((res) => {
        if (res.code === 200) {
          console.log('pfxs:', res)
          if (res.data != null) {
            this.form.fixation = res.data.fixation
            this.form.residualLiquor = res.data.residualLiquor
          }
        }
      })
      // 获取废气排放系数
      if (this.evaluate.faWzlb === '基础化学品' || this.evaluate.faWzlb === '助剂') {
        if (parseFloat(this.evaluate.lhsxbSrjd) < 100) {
          if (parseFloat(this.evaluate.lhsxbBhzqy) < 100) {
            this.form.airPfxs = 0.05
          } else if (parseFloat(this.evaluate.lhsxbBhzqy) >= 100 && parseFloat(this.evaluate.lhsxbBhzqy) < 1000) {
            this.form.airPfxs = 0.15
          } else {
            this.form.airPfxs = 0.4
          }
        } else if (parseFloat(this.evaluate.lhsxbSrjd) >= 100 && parseFloat(this.evaluate.lhsxbSrjd) < 1000) {
          if (parseFloat(this.evaluate.lhsxbBhzqy) < 100) {
            this.form.airPfxs = 0.025
          } else if (parseFloat(this.evaluate.lhsxbBhzqy) >= 100 && parseFloat(this.evaluate.lhsxbBhzqy) < 1000) {
            this.form.airPfxs = 0.05
          } else {
            this.form.airPfxs = 0.15
          }
        } else if (parseFloat(this.evaluate.lhsxbSrjd) >= 1000 && parseFloat(this.evaluate.lhsxbSrjd) < 10000) {
          if (parseFloat(this.evaluate.lhsxbBhzqy) < 100) {
            this.form.airPfxs = 0.01
          } else if (parseFloat(this.evaluate.lhsxbBhzqy) >= 100 && parseFloat(this.evaluate.lhsxbBhzqy) < 1000) {
            this.form.airPfxs = 0.025
          } else {
            this.form.airPfxs = 0.05
          }
        } else {
          if (parseFloat(this.evaluate.lhsxbBhzqy) < 100) {
            this.form.airPfxs = 0.005
          } else if (parseFloat(this.evaluate.lhsxbBhzqy) >= 100 && parseFloat(this.evaluate.lhsxbBhzqy) < 1000) {
            this.form.airPfxs = 0.01
          } else {
            this.form.airPfxs = 0.025
          }
        }
      } else {
        if (this.evaluate.faClgy === '浸染工艺') {
          this.form.airPfxs = 0.0007
        } else {
          this.form.airPfxs = 0.0025
        }
      }
      // 获取废渣排放系数
      // 物质类别为染料（着色剂）
      if (this.evaluate.faWzlb === '染料') {
        this.form.soilPfxs = 0.005
      } else {
        if (parseFloat(this.evaluate.lhsxbSrjd) < 100) {
          if (parseFloat(this.evaluate.lhsxbBhzqy) < 100) {
            this.form.soilPfxs = 0.05
          } else if (parseFloat(this.evaluate.lhsxbBhzqy) >= 100 && parseFloat(this.evaluate.lhsxbBhzqy) < 500) {
            this.form.soilPfxs = 0.15
          } else if (parseFloat(this.evaluate.lhsxbBhzqy) >= 500) {
            this.form.soilPfxs = 0.4
          }
        } else {
          if (parseFloat(this.evaluate.lhsxbBhzqy) < 100) {
            this.form.soilPfxs = 0.025
          } else if (parseFloat(this.evaluate.lhsxbBhzqy) >= 100 && parseFloat(this.evaluate.lhsxbBhzqy) < 500) {
            this.form.soilPfxs = 0.05
          } else if (parseFloat(this.evaluate.lhsxbBhzqy) >= 500) {
            this.form.soilPfxs = 0.15
          }
        }
      }
    },
    sendEmissionFactor() {
      if (this.form.fixation != '') {
        if (this.form.residualLiquor != '') {
          if (this.form.airPfxs != '') {
            if (this.form.soilPfxs != '') {
              // let flag = {flag:'1'};
              const emissionFactor = { fixation: this.form.fixation, fixationFlag: this.flag_1, fixationAccording: this.input_2, residualLiquor: this.form.residualLiquor, residualLiquorFlag: this.flag_2, residualLiquorAccording: this.input_4, airPfxs: this.form.airPfxs, airPfxsFlag: this.flag_3, airPfxsAccording: this.input_6, soilPfxs: this.form.soilPfxs, soilPfxsFlag: this.flag_4, soilPfxsAccording: this.input_8 }
              this.$store.commit('setEmissionFactor', emissionFactor)
              this.$emit('listenChild')
            } else {
              this.$message('废渣排放系数不能为空')
            }
          } else {
            this.$message('废气排放系数不能为空')
          }
        } else {
          this.$message('废水残液量不能为空')
        }
      } else {
        this.$message('废水固着度不能为空')
      }
    }
  }

}
</script>

<style scoped lang="scss">
  .title{
    position:absolute;
    left:0px;
  }
  .content{
    position:absolute;
    top:50px;
    left:0%;
    right:25%;
  }
</style>
